package com.twang.stock.service.impl;

import com.twang.stock.entity.DfcfBanKuaiMaoLiLv;
import com.twang.stock.entity.TodayQinXu;
import com.twang.stock.entity.ZhangTingBan;
import com.twang.stock.entity.ex.ZhangTingBanEx;
import com.twang.stock.mapper.ZhangTingBanMapper;
import com.twang.stock.service.ThsReDianStockService;
import com.twang.stock.service.ZhangTingBanService;
import com.twang.stock.service.ZhangTingBanTaskService;
import com.twang.stock.utils.date.HolidayCopy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ZhangTingBanServiceImpl implements ZhangTingBanService {
    public static String TYPE_SHORT_DESC="DESC";
    public static String TYPE_SHORT_ASC="ASC";

    public static String ZHANG_TING_BAN_DATA="ZHANG_TING_BAN_DATA";


    @Resource
    private ZhangTingBanMapper zhangTingBanMapper;

    @Autowired
    @Lazy
    private ZhangTingBanTaskService zhangTingBanTaskService;

    @Autowired
    @Lazy
    private ThsReDianStockService thsReDianStockService;

    @Override
    public List<ZhangTingBan> list(ZhangTingBan param, String type) {
        Example example = new Example(ZhangTingBan.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(param.getDate())) {
            criteria.andEqualTo("date", param.getDate());
        }
        if (StringUtils.isNotBlank(param.getMinutes())) {
            criteria.andEqualTo("minutes", param.getMinutes());
        }
        if (null!=param.getLbc()) {
            example.orderBy("lbc").desc();
        }
        if (TYPE_SHORT_DESC.equals(type)) {
            example.orderBy("id").desc();
        }
        if (TYPE_SHORT_ASC.equals(type)) {
            example.orderBy("id").asc();
        }
        List<ZhangTingBan> result = zhangTingBanMapper.selectByExample(example);
        //周末节假日没有数据，取最近工作日的日期
        if (CollectionUtils.isEmpty(result)) {
            Example example1 = new Example(TodayQinXu.class);
            Example.Criteria criteria1 = example.createCriteria();
            example1.orderBy("id").desc();
            param.setDate(zhangTingBanMapper.selectByExample(example1).get(0).getDate());
            return  list(param, type);
        }
        return result;
    }

    @Override
    public ZhangTingBanEx queryZhangTingBan(ZhangTingBan param, Map<String,List<ZhangTingBan>> ztbMap) throws Exception {
        if(StringUtils.isEmpty(param.getDate())){
            throw new Exception("請輸入日期!");
        }
        List<ZhangTingBan> result = zhangTingBanMapper.select(param);
        if(CollectionUtils.isNotEmpty(result)){
            ZhangTingBanEx zhangTingBanEx=new ZhangTingBanEx();
            List<ZhangTingBan> first=  result.stream().filter(ztb->ztb.getLbc().compareTo(new BigDecimal(1))==0).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(first)){
                zhangTingBanEx.setFirst(new BigDecimal(first.size()));
                //
                setFund(first);
                ztbMap.put(ZhangTingBanCommon.FIRST_一板,first);
            }else{
                zhangTingBanEx.setFirst(BigDecimal.ZERO);
                ztbMap.put(ZhangTingBanCommon.FIRST_一板,null);
            }

            List<ZhangTingBan> second=  result.stream().filter(ztb->ztb.getLbc().compareTo(new BigDecimal(2))==0).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(second)){
                zhangTingBanEx.setSecond(new BigDecimal(second.size()));
                //
                setFund(second);
                ztbMap.put(ZhangTingBanCommon.SECOND_二板,second);
            }else{
                zhangTingBanEx.setSecond(BigDecimal.ZERO);
                ztbMap.put(ZhangTingBanCommon.SECOND_二板,null);
            }

            List<ZhangTingBan> third=  result.stream().filter(ztb->ztb.getLbc().compareTo(new BigDecimal(3))==0).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(third)){
                zhangTingBanEx.setThird(new BigDecimal(third.size()));
                setFund(third);
                ztbMap.put(ZhangTingBanCommon.THIRD_三板,third);

            }else{
                zhangTingBanEx.setThird(BigDecimal.ZERO);
                ztbMap.put(ZhangTingBanCommon.THIRD_三板,null);

            }

            List<ZhangTingBan> fouth=  result.stream().filter(ztb->ztb.getLbc().compareTo(new BigDecimal(4))==0).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(fouth)){
                zhangTingBanEx.setFouth(new BigDecimal(fouth.size()));
                setFund(fouth);
                ztbMap.put(ZhangTingBanCommon.FOURTH_四板,fouth);

            }else{
                zhangTingBanEx.setFouth(BigDecimal.ZERO);
                ztbMap.put(ZhangTingBanCommon.FOURTH_四板,null);

            }

            List<ZhangTingBan> five=  result.stream().filter(ztb->ztb.getLbc().compareTo(new BigDecimal(5))==0).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(five)){
                zhangTingBanEx.setFive(new BigDecimal(five.size()));
                setFund(five);
                ztbMap.put(ZhangTingBanCommon.FIVE_五板,five);

            }else{
                zhangTingBanEx.setFive(BigDecimal.ZERO);
                ztbMap.put(ZhangTingBanCommon.FIVE_五板,null);

            }

            List<ZhangTingBan> more=  result.stream().filter(ztb->ztb.getLbc().compareTo(new BigDecimal(5))==1).collect(Collectors.toList());
            if(CollectionUtils.isNotEmpty(more)){
                zhangTingBanEx.setMore(new BigDecimal(more.size()));
                setFund(more);
                ztbMap.put(ZhangTingBanCommon.MORE_更高,more);

            }else{
                zhangTingBanEx.setMore(BigDecimal.ZERO);
                ztbMap.put(ZhangTingBanCommon.MORE_更高,null);

            }
            return zhangTingBanEx;
        }
        return null;
    }

    @Override
    public String zhangtingReason(String code) {
        try {
            Example example = new Example(ZhangTingBan.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("code", code.replace("sh", "").replace("sz", ""));
            example.orderBy("id").desc();
            List<ZhangTingBan> zhangTingBans = zhangTingBanMapper.selectByExample(example);
            if (!org.springframework.util.CollectionUtils.isEmpty(zhangTingBans)) {
                return zhangTingBans.get(0).getZtReason();
            }
        } catch (Exception e) {
            log.error("查询涨停原因失败！，e={}",e);
            throw e;
        }
        return null;

    }

    @Override
    public Boolean zhangtingDatahis(String code,String date) {
        try {
            Example example = new Example(ZhangTingBan.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("code", code.replace("sh", "").replace("sz", ""));
            criteria.andEqualTo("date", date);
            example.orderBy("id").desc();
            List<ZhangTingBan> zhangTingBans = zhangTingBanMapper.selectByExample(example);
            if (!org.springframework.util.CollectionUtils.isEmpty(zhangTingBans)) {
                return Boolean.TRUE;
            }
        } catch (Exception e) {
            log.error("查询涨停原因失败！，e={}",e);
            throw e;
        }
        return Boolean.FALSE;

    }



    /**
     * 初始化c
     * @param list
     */
    private void setFund(List<ZhangTingBan> list){
        list.stream().forEach(item->{
            String time=item.getLbt();
            if (time.length() == 5) {
                time="0"+time.substring(0,1)+":"+time.substring(1,3)+":"+time.substring(3,5);
            }else{
                time=time.substring(0,2)+":"+time.substring(2,4)+":"+time.substring(4,6);
            }
            item.setLbt(time);
            item.setFund(item.getFund().divide(new BigDecimal(100000000),2,BigDecimal.ROUND_HALF_UP));
        });
    }

    public static class ZhangTingBanCommon{
        public static String FIRST_一板="first";
        public static String SECOND_二板="second";
        public static String THIRD_三板="third";
        public static String FOURTH_四板="fourth";
        public static String FIVE_五板="five";
        public static String MORE_更高="more";
    }


    @Override
    public List<DfcfBanKuaiMaoLiLv>  queryZhangTingBanNew() {
        //
        ZhangTingBan zhangTingBanParam = new ZhangTingBan();
        zhangTingBanParam.setDate(HolidayCopy.getRealToday());
        zhangTingBanParam.setLbc(new BigDecimal(11));
        List<ZhangTingBan> list = this.list(zhangTingBanParam, null);
        List<DfcfBanKuaiMaoLiLv> dfcfBanKuaiMaoLiLvList= Lists.newArrayList();
        list.forEach(item->{
            try {
                DfcfBanKuaiMaoLiLv dfcfBanKuaiMaoLiLv= thsReDianStockService.stockComon(item.getCode(),item.getName());
                dfcfBanKuaiMaoLiLv.setZhangTingBan(item);
                dfcfBanKuaiMaoLiLvList.add(dfcfBanKuaiMaoLiLv);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        zhangTingBanTaskService.initKanDian(dfcfBanKuaiMaoLiLvList, Boolean.FALSE);
        //30 Min 成交额
        zhangTingBanTaskService.getTrade30MinutesVol(dfcfBanKuaiMaoLiLvList);

        return dfcfBanKuaiMaoLiLvList;
    }
}
